--
-- Author: shunguo.chen
-- Date: 2016-08-09 13:53:57
--
-- FriendRender

local HeadRender = require("app.widget.renders.HeadRender")

local ClassRef = app.mvc.createRenderClass(...)

--data = FriendVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.FriendNode1))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())

	-- head
	self._headRender = HeadRender.create(self._ui.head, 3)
	
	self._classIcon = self._ui["Panel/Panel_bg/icon2"]

	-- btns
	self._ui.Button_more:addClickEventListener(function()
		if not self._data then
			return
		end
		local pos = self._ui.Panel:convertToWorldSpace(cc.p(90,50))
		executeFunc(self._onBtnClick, "btnMsdkMore", {attr=self._data.info, pos=pos})
	end)
	
	self._ui["Panel/Panel_qq"]:setVisible(true)

	self._qqBtn1 = self._ui["Panel/btn_qq_1"]
    self._qqBtn2 = self._ui["Panel/btn_qq_2"]
    self._wxBtn = self._ui["Panel/btn_wx"]
    self._qqBtn1:addClickEventListener(handler(self, self._onClickQQGameWakeUp))
    self._qqBtn2:addClickEventListener(handler(self, self._onClickQQVip))
	self._wxBtn:addClickEventListener(handler(self, self._onClickWXGameWakeUp))
	
	self._startPosX = self._qqBtn1:getPositionX()
	

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self:setVisible(true)
		local nickName = self._data:getMsdkNickname()
		local name = self._data.name
		if nickName and nickName ~= "" then
			name = self._data.name .."("..nickName..")"
		end
		
		self:setName(self:_convertName(name))
		self:setOnline(true)--self._data.online)
		self:setHead(self._data)

		local duty = self._data:getDuty() or ""
		self._ui.combat_num:setString(string.format(L("lua_code_text_44"), self._data.info.level, duty))
		self._ui.Text_guild:setString(L(UD.zoneName))
		-- self._classIcon:loadTexture(self._data:getSpecialistIcon())

		local isWakeUpFromQQ = UD.msdkData:isWakeUpFromQQGame(self._data.wakeUpFrom)
		local isQQVip = UD.msdkData:isQQVip( self._data.msdkVip )
		self._qqBtn1:setVisible(isWakeUpFromQQ)
		self._qqBtn2:setVisible(isQQVip)
	
		local offsetX = 0
		if isWakeUpFromQQ then
			offsetX = self._qqBtn1:getContentSize().width
		end
		self._qqBtn2:setPositionX(self._startPosX + offsetX + 4)

		self._wxBtn:setVisible(UD.msdkData:isWakeUpFromWXGame(self._data.wakeUpFrom))

		local rank = self._data:getRank()
		self._ui["Panel/Panel_qq/no_1"]:setVisible(rank == 1)
		self._ui["Panel/Panel_qq/no_2"]:setVisible(rank == 2)
		self._ui["Panel/Panel_qq/no_3"]:setVisible(rank == 3)
		self._ui["Panel/Panel_qq/no_bg"]:setVisible(rank >3)
		self._ui["Panel/Panel_qq/num"]:setVisible(rank >3)
		self._ui["Panel/Panel_qq/num"]:setString(tostring(rank))
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

function ClassRef:setName(name)
	self._ui.Text_name:setString(name)
end

function ClassRef:setOnline(b)
	self._ui.mask:setVisible(not b)
end

function ClassRef:setJob(job)
	local path = GD:getClassIconPath(job, 2)
	self._ui["player/playericon/zhiye"]:loadTexture(path)
end

function ClassRef:setHead(data)
	local url, cacheName = data:getHeadUrl()
	if url and cacheName then
		self._headRender:setAvatarIconFromUrl(url, cacheName)
	end
	self._headRender:setData(data)
end

function ClassRef:setGift(b)
	-- self._ui.gift:setVisible(b)
end


function ClassRef:_onClickQQVip()
	UD.msdkData:qqVipClickTip()
end

function ClassRef:_onClickQQGameWakeUp()
	UD.msdkData:qqGameCenterClickTip()
end

function ClassRef:_onClickWXGameWakeUp()
	UD.msdkData:wxGameCenterClickTip()
end

function ClassRef:_convertName(name)
	local len = string.utf8len(name)
	if len > 9 then
		return string.usub(name, 1, 9-1) .. "..."
	else
		return name
	end
end
return ClassRef